const fs = require('fs');
const path = require('path');

const targetExtensions = ['.js', '.vue', '.json'];
const outputFile = path.join(process.cwd(), 'project_code_dump.txt');

let result = '';

function readFilesRecursively(dir) {
  const entries = fs.readdirSync(dir, { withFileTypes: true });

  for (const entry of entries) {
    const fullPath = path.join(dir, entry.name);

    if (entry.isDirectory()) {
      readFilesRecursively(fullPath);
    } else if (targetExtensions.includes(path.extname(entry.name))) {
      try {
        const content = fs.readFileSync(fullPath, 'utf-8');
        const relativePath = path.relative(process.cwd(), fullPath);
        result += `\n--- 文件路径: ${relativePath} ---\n${content}\n`;
      } catch (err) {
        console.error(`无法读取文件 ${fullPath}:`, err);
      }
    }
  }
}

// 开始执行
readFilesRecursively(process.cwd());

try {
  fs.writeFileSync(outputFile, result, 'utf-8');
  console.log(`✅ 所有文件内容已保存到 ${outputFile}`);
} catch (err) {
  console.error('❌ 写入文件失败:', err);
}
